Repository navigation
[swc-plugin] Compile a "use step" function expression bound to a variable inside a function as a nested step - #5503
Conversation
…le inside a function as a nested step
`const send = async function () { "use step"; ... }` inside a workflow (or
any function) took the module-level path in visit_mut_var_decl: step mode
registered nothing for it, and workflow mode called an un-namespaced step
ID without the closure variables, so the run failed with "Step ... is not
registered". It now takes the same path as the arrow form: hoisted,
namespaced by the enclosing function (`workflow/send`), and given the
variables it captures.
Adds a fixture for named and anonymous function expressions in a workflow
and in a plain helper (with a module-level control), a vitest workbench
runtime test that fails on the previous plugin, and a spec note.
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
🦋 Changeset detectedLatest commit: 9384c7d The changes in this PR will be included in the next version bump. This PR includes changesets to release 19 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
🧪 E2E Test Results✅ All tests passed
|
| Passed | Failed | Skipped | Total | |
|---|---|---|---|---|
| ✅ ▲ Vercel Production | 4649 | 0 | 940 | 5589 |
| ✅ 💻 Local Development | 5272 | 0 | 731 | 6003 |
| ✅ 📦 Local Production | 5272 | 0 | 731 | 6003 |
| ✅ 🐘 Local Postgres | 5417 | 0 | 586 | 6003 |
| ✅ 🪟 Windows | 401 | 0 | 13 | 414 |
| ✅ 🌐 Cross-language Conformance | 68 | 0 | 114 | 182 |
| ✅ dynamic-runs | 0 | 0 | 0 | 0 |
| ✅ local-postgres-spec9 | 0 | 0 | 364 | 364 |
| ✅ local-spec9 | 409 | 0 | 5 | 414 |
| ✅ vercel-http-transport | 1054 | 0 | 188 | 1242 |
| ✅ vercel-multi-region | 27 | 0 | 0 | 27 |
| ✅ vercel-queue-hold | 4 | 0 | 0 | 4 |
| ✅ vercel-spec9 | 395 | 0 | 19 | 414 |
| ✅ vercel-ws-transport | 712 | 0 | 116 | 828 |
| Total | 23680 | 0 | 3807 | 27487 |
Details by Category
✅ ▲ Vercel Production
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ astro-node | 171 | 0 | 36 |
| ✅ astro-quickjs | 170 | 0 | 37 |
| ✅ example-node | 171 | 0 | 36 |
| ✅ example-quickjs | 170 | 0 | 37 |
| ✅ express-node | 171 | 0 | 36 |
| ✅ express-quickjs | 170 | 0 | 37 |
| ✅ fastify-node | 171 | 0 | 36 |
| ✅ fastify-quickjs | 170 | 0 | 37 |
| ✅ hono-node | 171 | 0 | 36 |
| ✅ hono-quickjs | 170 | 0 | 37 |
| ✅ nest-node | 171 | 0 | 36 |
| ✅ nest-quickjs | 170 | 0 | 37 |
| ✅ nextjs-turbopack-node | 199 | 0 | 8 |
| ✅ nextjs-turbopack-quickjs | 198 | 0 | 9 |
| ✅ nextjs-webpack-node | 199 | 0 | 8 |
| ✅ nextjs-webpack-quickjs | 198 | 0 | 9 |
| ✅ nitro-node | 171 | 0 | 36 |
| ✅ nitro-quickjs | 170 | 0 | 37 |
| ✅ nuxt-node | 171 | 0 | 36 |
| ✅ nuxt-quickjs | 170 | 0 | 37 |
| ✅ python-node | 66 | 0 | 141 |
| ✅ sveltekit-node | 190 | 0 | 17 |
| ✅ sveltekit-quickjs | 189 | 0 | 18 |
| ✅ tanstack-start-node | 171 | 0 | 36 |
| ✅ tanstack-start-quickjs | 170 | 0 | 37 |
| ✅ vite-node | 171 | 0 | 36 |
| ✅ vite-quickjs | 170 | 0 | 37 |
✅ 💻 Local Development
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ astro-stable-node | 172 | 0 | 35 |
| ✅ astro-stable-quickjs | 171 | 0 | 36 |
| ✅ express-stable-node | 172 | 0 | 35 |
| ✅ express-stable-quickjs | 171 | 0 | 36 |
| ✅ fastify-stable-node | 172 | 0 | 35 |
| ✅ fastify-stable-quickjs | 171 | 0 | 36 |
| ✅ hono-stable-node | 172 | 0 | 35 |
| ✅ hono-stable-quickjs | 171 | 0 | 36 |
| ✅ nest-stable-node | 172 | 0 | 35 |
| ✅ nest-stable-quickjs | 171 | 0 | 36 |
| ✅ nextjs-turbopack-canary-node | 201 | 0 | 6 |
| ✅ nextjs-turbopack-canary-quickjs | 200 | 0 | 7 |
| ✅ nextjs-turbopack-quickjs-snapshot | 200 | 0 | 7 |
| ✅ nextjs-turbopack-stable-node | 201 | 0 | 6 |
| ✅ nextjs-turbopack-stable-quickjs | 200 | 0 | 7 |
| ✅ nextjs-webpack-canary-node | 201 | 0 | 6 |
| ✅ nextjs-webpack-canary-quickjs | 200 | 0 | 7 |
| ✅ nextjs-webpack-stable-node | 201 | 0 | 6 |
| ✅ nextjs-webpack-stable-quickjs | 200 | 0 | 7 |
| ✅ nitro-stable-node | 172 | 0 | 35 |
| ✅ nitro-stable-quickjs | 171 | 0 | 36 |
| ✅ nuxt-stable-node | 172 | 0 | 35 |
| ✅ nuxt-stable-quickjs | 171 | 0 | 36 |
| ✅ sveltekit-stable-node | 191 | 0 | 16 |
| ✅ sveltekit-stable-quickjs | 190 | 0 | 17 |
| ✅ tanstack-start-node | 172 | 0 | 35 |
| ✅ tanstack-start-quickjs | 171 | 0 | 36 |
| ✅ vite-stable-node | 172 | 0 | 35 |
| ✅ vite-stable-quickjs | 171 | 0 | 36 |
✅ 📦 Local Production
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ astro-stable-node | 172 | 0 | 35 |
| ✅ astro-stable-quickjs | 171 | 0 | 36 |
| ✅ express-stable-node | 172 | 0 | 35 |
| ✅ express-stable-quickjs | 171 | 0 | 36 |
| ✅ fastify-stable-node | 172 | 0 | 35 |
| ✅ fastify-stable-quickjs | 171 | 0 | 36 |
| ✅ hono-stable-node | 172 | 0 | 35 |
| ✅ hono-stable-quickjs | 171 | 0 | 36 |
| ✅ nest-stable-node | 172 | 0 | 35 |
| ✅ nest-stable-quickjs | 171 | 0 | 36 |
| ✅ nextjs-turbopack-canary-node | 201 | 0 | 6 |
| ✅ nextjs-turbopack-canary-quickjs | 200 | 0 | 7 |
| ✅ nextjs-turbopack-quickjs-snapshot | 200 | 0 | 7 |
| ✅ nextjs-turbopack-stable-node | 201 | 0 | 6 |
| ✅ nextjs-turbopack-stable-quickjs | 200 | 0 | 7 |
| ✅ nextjs-webpack-canary-node | 201 | 0 | 6 |
| ✅ nextjs-webpack-canary-quickjs | 200 | 0 | 7 |
| ✅ nextjs-webpack-stable-node | 201 | 0 | 6 |
| ✅ nextjs-webpack-stable-quickjs | 200 | 0 | 7 |
| ✅ nitro-stable-node | 172 | 0 | 35 |
| ✅ nitro-stable-quickjs | 171 | 0 | 36 |
| ✅ nuxt-stable-node | 172 | 0 | 35 |
| ✅ nuxt-stable-quickjs | 171 | 0 | 36 |
| ✅ sveltekit-stable-node | 191 | 0 | 16 |
| ✅ sveltekit-stable-quickjs | 190 | 0 | 17 |
| ✅ tanstack-start-node | 172 | 0 | 35 |
| ✅ tanstack-start-quickjs | 171 | 0 | 36 |
| ✅ vite-stable-node | 172 | 0 | 35 |
| ✅ vite-stable-quickjs | 171 | 0 | 36 |
✅ 🐘 Local Postgres
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ astro-stable-node | 177 | 0 | 30 |
| ✅ astro-stable-quickjs | 176 | 0 | 31 |
| ✅ express-stable-node | 177 | 0 | 30 |
| ✅ express-stable-quickjs | 176 | 0 | 31 |
| ✅ fastify-stable-node | 177 | 0 | 30 |
| ✅ fastify-stable-quickjs | 176 | 0 | 31 |
| ✅ hono-stable-node | 177 | 0 | 30 |
| ✅ hono-stable-quickjs | 176 | 0 | 31 |
| ✅ nest-stable-node | 177 | 0 | 30 |
| ✅ nest-stable-quickjs | 176 | 0 | 31 |
| ✅ nextjs-turbopack-canary-node | 206 | 0 | 1 |
| ✅ nextjs-turbopack-canary-quickjs | 205 | 0 | 2 |
| ✅ nextjs-turbopack-quickjs-snapshot | 205 | 0 | 2 |
| ✅ nextjs-turbopack-stable-node | 206 | 0 | 1 |
| ✅ nextjs-turbopack-stable-quickjs | 205 | 0 | 2 |
| ✅ nextjs-webpack-canary-node | 206 | 0 | 1 |
| ✅ nextjs-webpack-canary-quickjs | 205 | 0 | 2 |
| ✅ nextjs-webpack-stable-node | 206 | 0 | 1 |
| ✅ nextjs-webpack-stable-quickjs | 205 | 0 | 2 |
| ✅ nitro-stable-node | 177 | 0 | 30 |
| ✅ nitro-stable-quickjs | 176 | 0 | 31 |
| ✅ nuxt-stable-node | 177 | 0 | 30 |
| ✅ nuxt-stable-quickjs | 176 | 0 | 31 |
| ✅ sveltekit-stable-node | 196 | 0 | 11 |
| ✅ sveltekit-stable-quickjs | 195 | 0 | 12 |
| ✅ tanstack-start-node | 177 | 0 | 30 |
| ✅ tanstack-start-quickjs | 176 | 0 | 31 |
| ✅ vite-stable-node | 177 | 0 | 30 |
| ✅ vite-stable-quickjs | 176 | 0 | 31 |
✅ 🪟 Windows
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ nextjs-turbopack-node | 201 | 0 | 6 |
| ✅ nextjs-turbopack-quickjs | 200 | 0 | 7 |
✅ 🌐 Cross-language Conformance
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ python | 68 | 0 | 114 |
✅ dynamic-runs
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ astro-vercel | 0 | 0 | 0 |
| ✅ example-vercel | 0 | 0 | 0 |
| ✅ express-vercel | 0 | 0 | 0 |
| ✅ fastify-vercel | 0 | 0 | 0 |
| ✅ hono-vercel | 0 | 0 | 0 |
| ✅ nest-vercel | 0 | 0 | 0 |
| ✅ nextjs-turbopack-vercel | 0 | 0 | 0 |
| ✅ nextjs-webpack-vercel | 0 | 0 | 0 |
| ✅ nitro-vercel | 0 | 0 | 0 |
| ✅ nuxt-vercel | 0 | 0 | 0 |
| ✅ sveltekit-vercel | 0 | 0 | 0 |
| ✅ tanstack-start-vercel | 0 | 0 | 0 |
| ✅ vite-vercel | 0 | 0 | 0 |
✅ local-postgres-spec9
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ nextjs-turbopack-stable-node | 0 | 0 | 182 |
| ✅ nextjs-turbopack-stable-quickjs | 0 | 0 | 182 |
✅ local-spec9
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ nextjs-turbopack-node | 205 | 0 | 2 |
| ✅ nextjs-turbopack-quickjs | 204 | 0 | 3 |
✅ vercel-http-transport
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ example | 171 | 0 | 36 |
| ✅ express | 171 | 0 | 36 |
| ✅ hono | 171 | 0 | 36 |
| ✅ nextjs-turbopack | 199 | 0 | 8 |
| ✅ nitro | 171 | 0 | 36 |
| ✅ vite | 171 | 0 | 36 |
✅ vercel-multi-region
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ nextjs-turbopack | 27 | 0 | 0 |
✅ vercel-queue-hold
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ nextjs-turbopack | 4 | 0 | 0 |
✅ vercel-spec9
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ nextjs-turbopack-node | 198 | 0 | 9 |
| ✅ nextjs-turbopack-quickjs | 197 | 0 | 10 |
✅ vercel-ws-transport
| App | Passed | Failed | Skipped |
|---|---|---|---|
| ✅ example | 171 | 0 | 36 |
| ✅ express | 171 | 0 | 36 |
| ✅ nextjs-turbopack | 199 | 0 | 8 |
| ✅ vite | 171 | 0 | 36 |
📊 Workflow Benchmarkscommit Backend:
📈 STSO distribution vs main (inline / queue-hop histograms)1020 steps (inline) Cumulative STSO time: main 98927ms → this run 110205ms (Δ +11278ms, +11%) ℹ️ Metric definitions & methodologyThe collapsed STSO distribution section above buckets every step gap, split inline (same warm process — pure framework overhead) vs queue-hop (fresh process — dispatch, reinit, replay). Best/P75/P90/P99 deltas compare against the most recent benchmark run on Metrics — TTFS: time to first step body (in-deployment start() → first step body) · Fan-out TTFS: fan-out time to first step (in-deployment start() → first of the parallel step bodies to complete) · Fan-out TTLS: fan-out time to last step (in-deployment start() → last of the parallel step bodies to complete, i.e. when the Promise.all resolves) · STSO: step-to-step overhead (gap between consecutive step bodies) · WO: workflow overhead (whole-run time outside step bodies, in-deployment anchored) Scenarios — step: one trivial no-op step, no stream; no hooks, so the run stays in turbo mode (in-process fast path) · stream: one streaming step; no hooks, so the run stays in turbo mode (in-process fast path) · hook + stream: registers a hook before one step, which exits turbo mode (dispatch path) · 1020 steps: 1020 trivial sequential steps; STSO is measured between consecutive steps in the given step ranges, and WO is the whole-run overhead outside step bodies · Promise.all(100 steps): 100 trivial no-op steps started together in a single Promise.all; Fan-out TTFS is the first of them to complete and Fan-out TTLS the last, both from the in-deployment clientStart, so their gap is the spread the runtime adds across the fan-out 🔴 marks a percentile over its target (within target is left unmarked). Targets (p75/p90/p99, ms) — TTFS 200/300/600 All timestamps are deployment-side; runs are triggered in-deployment, so the CI runner and api.vercel.com sit outside every measured window. TTFS = Cold starts stay in the numbers (real bursty-workload latency, inflates P75+); Best is the warm floor. |
Sim WorldSimulated world deterministic testing for races. Traces 🟢 world-sim scenario book — 0 fail of 45 total
Full trace: |
About these numbersSizes are gzip; parentheses show the change against
|
TooTallNate
left a comment
There was a problem hiding this comment.
LGTM, nothing blocking.
The fix is right. The new !self.in_module_level branch for Expr::Fn matches the nested arrow path: it hoists via push_nested_step_function, namespaces with record_nested_step_name, and collects closure vars in both modes. Step IDs line up between step and workflow output. Module-level function expressions still take the old path. Passing false for the lexical-this flag is correct for a regular function.
What I checked locally:
cargo testintransform/passes, includingstep_id_consistencyon the new fixture.- I ran extra probe fixtures (not committed) for a few edge cases:
argumentsinside the step gets hoisted correctly.thisgets hoisted correctly, with no closure getter.- Same-named function-expression steps in a nested block get the
~1suffix correctly (wf2/b/wf2/b~1), and this also works mixed with arrow steps.
- CI and E2E are green.
Non-blocking follow-ups (both were already there before this PR, so they shouldn't hold it up):
- A named function expression that calls itself. In
const fact = async function inner(k) { "use step"; return ... await inner(k - 1); },innergets collected as a closure variable. The workflow output then emits() => ({ inner }), andinnerisn't in scope in the workflow body, so it throws a ReferenceError once the getter runs. The hoisted step function is also renamed (wf$fact), so the self-reference has nothing to bind to on the step side. The unbound form (return async function inner() { "use step"; ... inner() }) has the same problem onmain. Nestedfunction decl() {}self-recursion also gives() => ({ decl }). That one does resolve because of hoisting, but it's still an odd capture. It's worth a follow-up to leave a function expression's own name out of the closure collector (and keep it bound in the hoisted copy). - No async check. The comment above this branch says "Validate that it's async", but nothing checks it. A non-async
const f = function () { "use step" }inside a workflow compiles to a step without a diagnostic. The arrow path and the module-level path behave the same way, so this is a separate cleanup.
…able inside a function as a nested step (#5503) A `"use step"` function expression bound to a variable inside a workflow (or any other function) was compiled as a module-level step, so the run failed when it reached it: ```ts export async function receiptWorkflow(n: number, label: string) { 'use workflow'; const send = async function () { 'use step'; return `${label}:${n}`; }; return send(); // Step "step//./workflows/receipt//send" is not registered } ``` The arrow form (`const send = async () => { 'use step'; ... }`) and a function declaration already worked. **Cause:** in `visit_mut_var_decl`, the `Expr::Fn` step branch had no nested-scope check, unlike the `Expr::Arrow` branch next to it. As a result: - step mode emitted a registration for a binding that doesn't exist at module scope, so nothing was registered; - workflow mode called the un-namespaced step ID (`…//send`) without a closure-variable getter. **Fix:** inside a function, this branch now takes the same path as the arrow form. The step is hoisted and namespaced by the enclosing function (`receiptWorkflow/send`), it gets the variables it captures, and the original function is kept with the directive stripped for direct calls. A step bound to a variable takes the variable's name, as it already did at module scope and for arrows. Module-level function expressions are unchanged. - **New fixture** (`nested-step-function-expression`): named and anonymous function expressions inside a workflow, one inside a plain helper function, and a module-level control. The step and workflow outputs agree on every step ID, which `step_id_consistency` checks. All 421 Rust tests pass, and existing fixture outputs are unchanged. - **Runtime:** the vitest workbench runs a workflow with a named and an anonymous function-expression step, both capturing workflow variables. On the previous plugin build it fails with `Step "step//./workflows/nested-step-ids//named" is not registered`; with this change it passes. The full vitest workbench passes (77 tests, plus one marked as an expected failure). - **Spec:** `spec.md` lists the nested-step forms and how each is named. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> Co-authored-by: Pranay Prakash <1797812+pranaygp@users.noreply.github.com> Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
|
Backport PR opened against |
… bound to a variable inside a function as a nested step (#5509) Automated backport of #5503 to `stable` ([backport job run](https://github.com/vercel/workflow/actions/runs/44140977209)). **AI recommendation:** This fixes a bug in a feature `stable` already has. A `"use step"` function expression bound to a variable inside a workflow is compiled as a module-level step, so the run fails with "Step ... is not registered". The same un-scoped `Expr::Fn` branch in `visit_mut_var_decl` exists on `stable`, which already supports nested arrow steps and closure variables. The plugin code on `stable` has diverged from `main` (different helper signatures and a `Client` mode), so expect the cherry-pick to conflict and review it carefully. Merge conflicts were resolved by AI ([opencode](https://opencode.ai) with `anthropic/claude-opus-5.5`). **Please review the conflict resolution carefully before merging.** Signed-off-by: Pranay Prakash <pranay.gp@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> Co-authored-by: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
Now that a "use step" function expression bound to a variable compiles as a nested step (#5503), drop the advice to use only arrow functions and function declarations, and say how each form is named. Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Pranay Prakash <1797812+pranaygp@users.noreply.github.com>
Summary
A
"use step"function expression bound to a variable inside a workflow (or any other function) was compiled as a module-level step, so the run failed when it reached it:The arrow form (
const send = async () => { 'use step'; ... }) and a function declaration already worked.Cause: in
visit_mut_var_decl, theExpr::Fnstep branch had no nested-scope check, unlike theExpr::Arrowbranch next to it. As a result:…//send) without a closure-variable getter.Fix: inside a function, this branch now takes the same path as the arrow form. The step is hoisted and namespaced by the enclosing function (
receiptWorkflow/send), it gets the variables it captures, and the original function is kept with the directive stripped for direct calls. A step bound to a variable takes the variable's name, as it already did at module scope and for arrows. Module-level function expressions are unchanged.Tests
nested-step-function-expression): named and anonymous function expressions inside a workflow, one inside a plain helper function, and a module-level control. The step and workflow outputs agree on every step ID, whichstep_id_consistencychecks. All 421 Rust tests pass, and existing fixture outputs are unchanged.Step "step//./workflows/nested-step-ids//named" is not registered; with this change it passes. The full vitest workbench passes (77 tests, plus one marked as an expected failure).spec.mdlists the nested-step forms and how each is named.🤖 Generated with Claude Code